﻿/*==============================================
 * God Corporation S.A.C
 * 
 * Project: Finantial ERP System Application.
 * Developed by: Aarón Ibáñez W.
 * Team Role: SOA Architect  
 * Created date: 15/04/2008
 ===============================================
 * Traking Changes
  --------------------------------------------------------------------------------------
 * Track1: [ticket name which produce changes.]
 *  >> Modified by:
 *  >> Team Role:
 *  >> Modified date:
 *  >> Reasons:
 *      1.
 *      2.
 *      3.
 * --------------------------------------------------------------------------------------
 * Track2: [ticket name which produce changes.]
 *  >> Modified by:
 *  >> Team Role:
 *  >> Modified date:
 *  >> Reasons:
 *      1.
 *      2.
 *      3.
 * --------------------------------------------------------------------------------------*/

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Shapes;
using ActiproSoftware.Windows.Controls.Ribbon.Controls;
using System.Data;
using System.Drawing;
using ERP.Common.UIControls.Helpers;
using EnterpriseERP.Production.DiamondBitType.Entities;
using EnterpriseERP.Production.DiamondBitType.Screens;
using ERP.Common;
using System.Collections;
using System.Data.Linq;
using ActiproSoftware.Windows.Controls.Docking;
using ActiproSoftware.Windows.Controls.Navigation.Themes;
using ActiproSoftware.Windows.Themes;
using EnterpriseERP.Controller.ContainerFactory;

namespace Finantial.Boyles.ERP
{
    /// <summary>
    /// Interaction logic for winMain.xaml.
    /// </summary>
    public partial class winMain : Window
    {
        private System.Drawing.Size lvImageSmallSize = new System.Drawing.Size(20, 20);
        private System.Drawing.Size lvButtonSize = new System.Drawing.Size(26, 26);
        private int i = 0;
        private LinqEntitiesDataContext context = new LinqEntitiesDataContext();
        private winMasterTabCrudDetails wsd = null;

        /// <summary>
        /// Generic Constructor
        /// </summary>
        public winMain()
        {
            InitializeComponent();
            this.LoadTreeModules(this.DummyModulesData(), "0", null);
            ActiproSoftware.Windows.Themes.ThemeManager.CurrentTheme = "AeroNormalColor";
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            #region "Images in the Page"
            //ImagesButtons Configuration
            this.btnBack.VariantSize = VariantSize.Small;
            this.btnBack.Height = lvButtonSize.Height;
            this.btnBack.Width = lvButtonSize.Width;
            this.btnBack.ImageSourceSmall = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.Back, lvImageSmallSize);
            this.btnNext.VariantSize = VariantSize.Small;
            this.btnNext.Height = lvButtonSize.Height;
            this.btnNext.Width = lvButtonSize.Width;
            this.btnNext.ImageSourceSmall = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.Forward, lvImageSmallSize);
            this.btnNewSession.VariantSize = VariantSize.Small;
            this.btnNewSession.Height = lvButtonSize.Height;
            this.btnNewSession.Width = lvButtonSize.Width;
            this.btnNewSession.ImageSourceSmall = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.Session, lvImageSmallSize);
            this.btnLogout.VariantSize = VariantSize.Small;
            this.btnLogout.Height = lvButtonSize.Height;
            this.btnLogout.Width = lvButtonSize.Width;
            this.btnLogout.ImageSourceSmall = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.Logout, lvImageSmallSize);
            this.btnHelp.VariantSize = VariantSize.Small;
            this.btnHelp.Height = lvButtonSize.Height;
            this.btnHelp.Width = lvButtonSize.Width;
            this.btnHelp.ImageSourceSmall = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.Help2, lvImageSmallSize);
            #endregion
            //BackGround COnfiguration
            //this.imgBackground.Source = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.BoylesBros, ERP.Resources.Properties.Resources.BoylesBros.Size);
        }
        #region "Load TreeView"

        /// <summary>
        /// Dummy Modules Data is used to create the options to access to the program
        /// </summary>
        /// <returns></returns>
        private DataTable DummyModulesData()
        {
            DataTable lvDtb = new DataTable();
            lvDtb.Columns.AddRange(new DataColumn[] 
                    { 
                          new DataColumn("module_id", typeof(int)), 
                          new DataColumn("name", typeof(string)), 
                          new DataColumn("tooltip", typeof(string)),
                          new DataColumn("parent_id", typeof(int))
                    });

            int lvID = 0;
            //for (int i = 1; i < 7; i++)
            //{
            //    string strI = i.ToString();
            //    FileConverter lvFc = new FileConverter();
            //    lvID++;
            //    lvDtb.Rows.Add(new Object[] { lvID, "Mod" + strI, "tipMod" + strI, 0 });
            //    int lvID1 = lvID;
            //    for (int j = 1; j < 5; j++)
            //    {
            //        string strJ = strI + j.ToString();
            //        lvID++;
            //        lvDtb.Rows.Add(new Object[] { lvID, "Mod" + strJ, "tipMod" + strJ, lvID1 });
            //        int lvID2 = lvID;

            //        for (int k = 1; k < 3; k++)
            //        {
            //            string strK = strJ + k.ToString();
            //            lvID++;
            //            lvDtb.Rows.Add(new Object[] { lvID, "Mod" + strK, "tipMod" + strK, lvID2 });
            //        }
            //    }
            //}
            for (int i = 1; i < 2; i++)
            {
                string strI = i.ToString();
                FileConverter lvFc = new FileConverter();
                lvID++;
                lvDtb.Rows.Add(new Object[] { lvID, "DiamondBitMasterTables", "Production", 0 });
                int lvID1 = lvID;
                string[] lvTreeViewItemsList = Enum.GetNames(typeof(CrudTreeViewItemsEnum));

                foreach (string lvItem in lvTreeViewItemsList)
                {
                    lvID++;
                    lvDtb.Rows.Add(new Object[] { lvID, "Production." + lvItem.ToString(), "DobleClick to Select", lvID1 });
                    int lvID2 = lvID;
                }
            }
            lvDtb.AcceptChanges();
            return lvDtb;
        }

        /// <summary>
        /// Load the tree modules
        /// </summary>
        /// <param name="lpData"></param>
        /// <param name="lpParentID"></param>
        /// <param name="lpNode"></param>
        protected void LoadTreeModules(DataTable lpData, string lpParentID, WpfTreeViewItem lpNode)
        {
            DataView lvDtv = new DataView(lpData);
            lvDtv.RowFilter = "parent_id=" + lpParentID;
            // TreeView population...
            foreach (DataRowView lvRow in lvDtv)
            {
                System.Drawing.Size lvImageTvSize = new System.Drawing.Size(16,16);
                WpfTreeViewItem lvNewItem = null;
                ImageSourceConverter lvConv = new ImageSourceConverter();
                FileConverter lvFc = new FileConverter();
                
                if (lpNode == null)
                    this.tvModules.Items.Add(lvNewItem = new WpfTreeViewItem());
                else
                    lpNode.Items.Add(lvNewItem = new WpfTreeViewItem());
                lvNewItem.Text = lvRow["name"].ToString();
                lvNewItem.Name = lvRow["name"].ToString().Replace("Production.","");
                lvNewItem.ToolTip = lvRow["tooltip"].ToString();
                lvNewItem.IsExpanded = (lpNode == null);
                lvNewItem.UnselectedImage = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.folder_closed, lvImageTvSize);
                lvNewItem.SelectedImage = FileConverter.BitmapToImageSource(ERP.Resources.Properties.Resources.folder_open, lvImageTvSize);
                lvNewItem.MouseDoubleClick += new MouseButtonEventHandler(lvNewItem_MouseDoubleClick);
                this.LoadTreeModules(lpData, lvRow["module_id"].ToString(), lvNewItem);
            }
        }

        #endregion

        #region "Public Events"

        private void lvNewItem_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (i == 0) i++;
            else
            {
                i--;
                wsd = new winMasterTabCrudDetails();
                wsd.Owner = this;
                ISingleResult<usps_CurrentControlResult> lvCurrentControl = this.context.usps_CurrentControl(((WpfTreeViewItem)(this.tvModules.SelectedValue)).Text);
                string lvNameControl = string.Empty;
                foreach(var lvItem in lvCurrentControl)
                {
                    lvNameControl = lvItem.control_type;
                    wsd.LoadMyUserControls(lvNameControl);
                }
                string lvTitleControl = ((WpfTreeViewItem)(this.tvModules.SelectedValue)).Text;
                wsd.LoadDataGridControl(lvTitleControl);
                wsd.Title = lvTitleControl;
                wsd.Show();
                //ucTest lvTest = new ucTest();


                //ToolWindow lvToolWin = new ToolWindow(this.dksContainer);
                //lvToolWin.Title = lvTitleControl;
                //lvToolWin.HorizontalContentAlignment = HorizontalAlignment.Left;
                //lvToolWin.VerticalContentAlignment = VerticalAlignment.Top;
                ////lvToolWin.Content = 
                //this.MdiContainer.Items.Add(lvToolWin);

                Window lvWin = ErpContainerFactory<Window>.CreateContainer(this.dksContainer, lvNameControl, lvTitleControl);
                lvWin.Owner = this;
                lvWin.Show();

                this.MdiContainer.Items.Add(ErpContainerFactory<ToolWindow>.CreateContainer(this.dksContainer, lvNameControl, lvTitleControl));

            }
        }
        private void btnNewSession_Click(object sender, ActiproSoftware.Windows.Controls.Ribbon.Controls.ExecuteRoutedEventArgs e)
        {
            winMain lvNewWindow = new winMain();
            lvNewWindow.Title = "Finantial Boyles ERP";
            lvNewWindow.Show();
        }
        
        private void btnLogout_Click(object sender, ExecuteRoutedEventArgs e)
        {
            //winMasterCrud MasterCrud = new winMasterCrud();
            //MasterCrud.EntityObject = context.Sequences;
            //MasterCrud.SelectTableName = "Production.Od";
            //MasterCrud.Show();
            if(MessageBox.Show("Are You Sure to Close the Aplication?","Message",MessageBoxButton.YesNo,MessageBoxImage.Exclamation) == MessageBoxResult.Yes)
            {
                if (this.wsd != null)
                {
                    this.wsd.Close();
                    this.wsd = null;
                }
                this.Close();
            }
        }

        private void btnHelp_Click(object sender, ExecuteRoutedEventArgs e)
        {
            //winMasterTabCrud MasterTabCrud = new winMasterTabCrud();
            //MasterTabCrud.EntityObject = context.Sequences;
            //MasterTabCrud.SelectTableName = "Production.Od";
            //MasterTabCrud.Show();            

            //winAlternate win = new winAlternate();
            //win.Owner = this;
            //win.Show();

            //DocumentWindow lv = new DocumentWindow(this.dockSite);
                        
            //lv.Title = win.Title;
            ////lv.con
            //win.Show();
            //lv.Activate();

            winTheme winThemeColor = new winTheme();
            winThemeColor.Show();
        }

        #endregion
    }
}
